<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/xizhongren8980/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/xizhongren8980/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/xizhongren8980/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/xizhongren8980/images/logo.svg" color="#222">

<link rel="stylesheet" href="/xizhongren8980/css/main.css">


<link rel="stylesheet" href="/xizhongren8980/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/xizhongren8980/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1、复制数组Java标准库提供的 static 方法 System.arraycopy()，使用该方法复制数组比较方便，并且对所有类型做了重载。">
<meta property="og:type" content="article">
<meta property="og:title" content="有关Arrays方面的实用功能">
<meta property="og:url" content="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/03/06/%E6%9C%89%E5%85%B3Arrays%E6%96%B9%E9%9D%A2%E7%9A%84%E5%AE%9E%E7%94%A8%E5%8A%9F%E8%83%BD/index.html">
<meta property="og:site_name" content="一杯茶一包烟，一个BUG改一天">
<meta property="og:description" content="1、复制数组Java标准库提供的 static 方法 System.arraycopy()，使用该方法复制数组比较方便，并且对所有类型做了重载。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-06T11:51:08.673Z">
<meta property="article:modified_time" content="2020-01-26T04:49:46.980Z">
<meta property="article:author" content="MisterTian">
<meta property="article:tag" content="数组">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Arrays">
<meta property="article:tag" content="基础知识">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/03/06/%E6%9C%89%E5%85%B3Arrays%E6%96%B9%E9%9D%A2%E7%9A%84%E5%AE%9E%E7%94%A8%E5%8A%9F%E8%83%BD/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>有关Arrays方面的实用功能 | 一杯茶一包烟，一个BUG改一天</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/xizhongren8980/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">一杯茶一包烟，一个BUG改一天</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">收藏永不停止，学习永不开始</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/xizhongren8980/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/xizhongren8980/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/xizhongren8980/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/xizhongren8980/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/xizhongren8980/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/03/06/%E6%9C%89%E5%85%B3Arrays%E6%96%B9%E9%9D%A2%E7%9A%84%E5%AE%9E%E7%94%A8%E5%8A%9F%E8%83%BD/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/xizhongren8980/images/head.jpg">
      <meta itemprop="name" content="MisterTian">
      <meta itemprop="description" content="一个小菜鸟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一杯茶一包烟，一个BUG改一天">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          有关Arrays方面的实用功能
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-06 19:51:08" itemprop="dateCreated datePublished" datetime="2020-03-06T19:51:08+08:00">2020-03-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-01-26 12:49:46" itemprop="dateModified" datetime="2020-01-26T12:49:46+08:00">2020-01-26</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="1、复制数组"><a href="#1、复制数组" class="headerlink" title="1、复制数组"></a>1、复制数组</h3><p>Java标准库提供的 static 方法 <code>System.arraycopy()</code>，使用该方法复制数组比较方便，并且对所有类型做了重载。</p>
<a id="more"></a>
<pre><code>void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);</code></pre><p>其中 <code>src</code>表示需要复制的数组，<code>srcPos</code>表示源数组进行开始复制的下标，<code>dest</code>表示复制内容到哪里去的数组，即目标数组，<code>destPos</code>表示复制到数组的哪个下标位置，<code>length</code>表示复制的元素个数。</p>
<p><strong>代码示例</strong></p>
<pre>
import java.util.Arrays;

public class CopyArrays {
    public static void main(String[] args) {
        int[] a1 = {1,2,3,4,5};
        int[] a2 = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        System.out.println("a1 :" + Arrays.toString(a1));
        System.out.println("a2 :" + Arrays.toString(a2));
        System.arraycopy(a2,0,a1,0,a1.length);//数组a1的元素发生改变
        System.arraycopy(a1,3,a2,1,2);//数组a2的元素发生改变
        System.out.println("改变后的a1 :" + Arrays.toString(a1));
        System.out.println("改变后的a2 :" + Arrays.toString(a2));
    }
}
</pre>

<p><strong>输出结果</strong></p>
<pre>
a1 :[1, 2, 3, 4, 5]
a2 :[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
改变后的a1 :[6, 7, 8, 9, 10]
改变后的a2 :[6, 9, 10, 9, 10, 11, 12, 13, 14, 15]
</pre>

<p>该方法对基本类型数组和对象数组都可以复制，但是如果复制对象数组，那么只是复制了对象的引用，而不是对象本身的拷贝，这是浅复制。另外该方法不会执行自动包装和自动拆包，两个数组必须是具有相同的确切类型。</p>
<h3 id="2、数组的比较"><a href="#2、数组的比较" class="headerlink" title="2、数组的比较"></a>2、数组的比较</h3><p>数组的相等条件是元素个数必须相等，并且对应位置的元素也相等。</p>
<p><strong>代码示例</strong></p>
<pre>
import java.util.Arrays;
public class CopyArrays {
    public static void main(String[] args) {
        int[] a1 = {1,2,3,4,5};
        int[] a2 = {1,2,3,4,5};
        System.out.println(Arrays.equals(a1, a2));
        a1[0] = 11;
        System.out.println(Arrays.equals(a1, a2));
        String[] s1 = new String[3];
        Arrays.fill(s1,"hi");
        String[] s2 = {new String("hi"), new String("hi"), new String("hi")};
        System.out.println(Arrays.equals(s1, s2));
    }
}
</pre>

<p><strong>输出结果</strong></p>
<pre>
true
false
true
</pre>

<p>由s1和s2的比较可知道，数组的相等是基于内容的，通过<code>Object.equals()比较</code>。</p>
<h3 id="3、数组的排序"><a href="#3、数组的排序" class="headerlink" title="3、数组的排序"></a>3、数组的排序</h3><p>使用内置的排序方法，就可以对任意的基本类型数组排序；也可以对任意的对象数组进行排序，只要该对象实现了<code>Comparable</code>接口或具有相关关联的<code>Comparator</code>。 </p>
<p>下面的例子对两种基本类型进行排序演示。</p>
<p><strong>代码示例</strong></p>
<pre>
import java.util.Arrays;
public class CopyArrays {
    public static void main(String[] args) {
        int[] a1 = {5, 100, 8, 9, 15, 6, 70, 80};
        System.out.println("a1 before sort:" + Arrays.toString(a1));
        Arrays.sort(a1);
        System.out.println("a1 after sort:" + Arrays.toString(a1));
        String[] strings = {"YNzbr", "nyGcF", "OWZnT", "eGZMm", "EqUCB"};
        System.out.println("strings before sort:" + Arrays.toString(strings));
        Arrays.sort(strings);
        System.out.println("strings after sort:" + Arrays.toString(strings));
        Arrays.sort(strings,String.CASE_INSENSITIVE_ORDER);
        System.out.println("strings after CASE_INSENSITIVE_ORDER sort:" + Arrays.toString(strings));
    }
}

</pre>

<p><strong>输出结果</strong></p>
<pre>
a1 before sort:[5, 100, 8, 9, 15, 6, 70, 80]
a1 after sort:[5, 6, 8, 9, 15, 70, 80, 100]
strings before sort:[YNzbr, nyGcF, OWZnT, eGZMm, EqUCB]
strings after sort:[EqUCB, OWZnT, YNzbr, eGZMm, nyGcF]
strings after CASE_INSENSITIVE_ORDER sort:[eGZMm, EqUCB, nyGcF, OWZnT, YNzbr]
</pre>

<p>注意，<strong>String</strong>的排序算法是依据<strong>词典编排</strong>顺序排序的，所以大写字母开头的词都放在前面输出，然后才是小写字母开头的词。使用参数<code>CASE_INSENSITIVE_ORDER</code>可以忽略大小写。</p>
<h3 id="4、在已排序的数组中查找"><a href="#4、在已排序的数组中查找" class="headerlink" title="4、在已排序的数组中查找"></a>4、在已排序的数组中查找</h3><p>如果数组已经排好序，就可以使用<code>Arrays.binarySearch()</code>执行快速查找。</p>
<p><strong>代码示例</strong></p>
<pre>
import java.util.Arrays;
public class CopyArrays {
    public static void main(String[] args) {
        int[] a = new int[30];
        int k = 0;
        for (int i = 0; i < 30; i++) {
            k = i*5+2;
            a[i] = k;
        }
        System.out.println(Arrays.toString(a));
        int index = Arrays.binarySearch(a,112);
        System.out.println("Index of 122 is " + index);
    }
}
</pre>

<p><strong>输出结果</strong></p>
<pre>
[2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 82, 87, 92, 97, 102, 107, 112, 117, 122, 127, 132, 137, 142, 147]
Index of 122 is 22
</pre>

<p>如果找到了目标值，<code>Arrays.binarySearch()</code>产生的返回值就是在数组中的下标值，否则，返回的就是负值。如果数组包含重复的元素，，则无法保证找到的是这些相同值中的哪一个。虽然仍然可以用，但是不建议用。如果需要对没有重复元素的数组进行排序，可以选择使用<code>TreeSet</code>。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/xizhongren8980/tags/%E6%95%B0%E7%BB%84/" rel="tag"># 数组</a>
              <a href="/xizhongren8980/tags/Java/" rel="tag"># Java</a>
              <a href="/xizhongren8980/tags/Arrays/" rel="tag"># Arrays</a>
              <a href="/xizhongren8980/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag"># 基础知识</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/xizhongren8980/2020/03/06/Hexo%E9%83%A8%E7%BD%B2/" rel="prev" title="Hexo个人博客部署到Github">
      <i class="fa fa-chevron-left"></i> Hexo个人博客部署到Github
    </a></div>
      <div class="post-nav-item">
    <a href="/xizhongren8980/2020/03/06/Hexo%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA&&%E9%81%BF%E5%9D%91/" rel="next" title="Hexo博客零基础搭建教程">
      Hexo博客零基础搭建教程 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、复制数组"><span class="nav-number">1.</span> <span class="nav-text">1、复制数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、数组的比较"><span class="nav-number">2.</span> <span class="nav-text">2、数组的比较</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、数组的排序"><span class="nav-number">3.</span> <span class="nav-text">3、数组的排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、在已排序的数组中查找"><span class="nav-number">4.</span> <span class="nav-text">4、在已排序的数组中查找</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="MisterTian"
      src="/xizhongren8980/images/head.jpg">
  <p class="site-author-name" itemprop="name">MisterTian</p>
  <div class="site-description" itemprop="description">一个小菜鸟</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/xizhongren8980/archives/">
        
          <span class="site-state-item-count">57</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/xizhongren8980/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/xizhongren8980/tags/">
          
        <span class="site-state-item-count">101</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://mistertian8980.github.io/" title="https:&#x2F;&#x2F;mistertian8980.github.io&#x2F;" rel="noopener" target="_blank">Github博客</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MisterTian</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/xizhongren8980/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/xizhongren8980/lib/anime.min.js"></script>
  <script src="/xizhongren8980/lib/velocity/velocity.min.js"></script>
  <script src="/xizhongren8980/lib/velocity/velocity.ui.min.js"></script>

<script src="/xizhongren8980/js/utils.js"></script>

<script src="/xizhongren8980/js/motion.js"></script>


<script src="/xizhongren8980/js/schemes/pisces.js"></script>


<script src="/xizhongren8980/js/next-boot.js"></script>


  <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/three.min.js"></script>
    <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/canvas_lines.min.js"></script>


  




  
<script src="/xizhongren8980/js/local-search.js"></script>













  

  

</body>
</html>
